package com.payment.common.Aliconfig;


import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.payment.common.utils.UUIDGenerator;
import com.payment.common.wxutils.PayCommonUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.SortedMap;

/**
 * @author Doushaofei
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig {
    private String appId;//服务号的应用ID
    private String aliPayPrivateKey;// 应用私钥
    private String publicKey ; // 3.支付宝应用公钥
	private String aliPayPublicKey;//阿里应用公钥
    private String notifyUrl ;  // 4.回调接口全路径(支付完成异步通知)
    private String returnUrl ; // 5.页面跳转同步通知页面路径(支付完成后跳转的页面)
    private String gateWayUrl; // 6.请求支付宝的网关地址
    private String charset ; // 7.编码
    private String format; // 8.返回格式
    private String signType ;// 9.加密类型 签名加密方式
    private String quiteUrl; //支付取消、中断跳转页面URL
    private String timeOutExpress; //支付等待保持时间，过期结束
    private String mchId;// 商户号
    private String appCertPath;
	private String aliPayCertPath;
	private String aliPayRootCertPath;


    /**
     * 基础参数
     *
     * @author hongyang.jiang
     */
    public static void commonParams(SortedMap<Object, Object> packageParams) {
        // 生成随机字符串
        String currTime = PayCommonUtil.getCurrTime();
        String strTime = currTime.substring(8, currTime.length());
        String strRandom = PayCommonUtil.buildRandom(4) + "";
        String nonce_str = strTime + strRandom;
        packageParams.put("nonce_str", nonce_str);// 随机字符串
    }

    /**
     * 基础参数
     *
     * @author hongyang.jiang
     */
    public static String getNoncestr() {
        // 生成随机字符串
        String currTime = PayCommonUtil.getCurrTime();
        String strTime = currTime.substring(8, currTime.length());
        String strRandom = PayCommonUtil.buildRandom(4) + "";
        String noncestr = strTime + strRandom;
        return UUIDGenerator.getUUID();
    }

	public  boolean isBlank(String str) {
		int length;
		if (str != null && (length = str.length()) != 0) {
			for(int i = 0; i < length; ++i) {
				if (!Character.isWhitespace(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return true;
		}
	}
	public AlipayClient afterPropertiesSet() throws Exception {
		// 请求方式 json
		String FORMAT = "json";
		// 编码格式，目前只支持UTF-8
		String CHARSET = "UTF-8";
		// 签名方式
		String SIGN_TYPE = "RSA2";
		// 网关
		String URL = gateWayUrl;
		// 商户APP_ID
		String appId1 = appId;
		// 商户私钥
		String alipayPrivateKey = aliPayPrivateKey;
		// 应用公钥证书地址
		String appCertPath = aliPayCertPath;
		// 支付宝公钥证书地址
		String alipayCertPath = aliPayCertPath;
		// 支付宝根钥证书地址
		String alipayRootCertPath = aliPayRootCertPath;
		//构造client
		CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
		//设置网关地址
		certAlipayRequest.setServerUrl(URL);
		//设置应用Id
		certAlipayRequest.setAppId(appId);
		//设置应用私钥
		certAlipayRequest.setPrivateKey(alipayPrivateKey);
		//设置请求格式，固定值json
		certAlipayRequest.setFormat("json");
		//设置字符集
		certAlipayRequest.setCharset(CHARSET);
		//设置签名类型
		certAlipayRequest.setSignType(SIGN_TYPE);
		//设置应用公钥证书路径
		certAlipayRequest.setCertPath(alipayCertPath);
		//设置支付宝公钥证书路径
		certAlipayRequest.setAlipayPublicCertPath(aliPayPublicKey);
		//设置支付宝根证书路径
		certAlipayRequest.setRootCertPath(alipayRootCertPath);
		//构造client
		AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
		log.info("创建支付宝网关访问客户端完成, 网关地址：{}，appId:{}", URL, appId);
		return alipayClient;
	}


}
